package com.gopay.gass.service.impl;

import com.gopay.gass.exception.GassServiceException;
import com.gopay.gass.service.GassPayBankSelectorService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * Created by HJY on 2015/3/23.
 * 付款银行选择
 * 默认通过收款银行即为付款银行
 */
@Service("gassPayBankSelectorService")
public class GassPayBankSelectorServiceImpl implements GassPayBankSelectorService {

    @Value("#{bankConfig['GASS_BANK']}")
    private  String gassBanks;

    @Override
    public String getPayBank(String recvBankName)  throws GassServiceException{
        String bankCode = getBankCode(recvBankName);

        if(!Arrays.asList(gassBanks.split("\\|")).contains(bankCode))
            throw new GassServiceException("该银行不支持自动结算-"+bankCode);

        return bankCode;
    }

    private String getBankCode(String recvBankName) throws GassServiceException{
        if(StringUtils.isEmpty(recvBankName))
            throw new GassServiceException("收款银行为空");

        if(recvBankName.indexOf("工商")!=-1 || recvBankName.indexOf("工行")!=-1){
            return "ICBC";
        }

        if(recvBankName.indexOf("招商")!=-1 || recvBankName.indexOf("招行")!=-1){
            return "CMB";
        }
        
        if(recvBankName.indexOf("农业")!=-1 || recvBankName.indexOf("农行")!=-1){
        	return "ABC";
        }
        
        if(recvBankName.indexOf("平安")!=-1){
        	return "PAB";
        }
        
        throw new GassServiceException("收款银行不支持自动结算");
    }
}
